package com.ujiuye.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ujiuye.mapper.RoleMapper;
import com.ujiuye.mapper.RolePermissionMapper;
import com.ujiuye.pojo.Role;
import com.ujiuye.pojo.RolePermission;
import com.ujiuye.service.IRoleService;
import com.ujiuye.utils.MessageConstant;
import com.ujiuye.utils.PageResult;
import com.ujiuye.utils.QueryPageBean;
import com.ujiuye.utils.Result;
import com.ujiuye.vo.RolePermissionVo;
import org.apache.dubbo.config.annotation.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 * 角色 服务实现类
 * </p>
 *
 * @author zhangwenping
 * @since 2021-07-05
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {

    @Resource
    private RoleMapper roleMapper;
    @Resource
    private RolePermissionMapper rolePermissionMapper;


    @Override
    public PageResult listPage(QueryPageBean queryPageBean) {
        //创建分页对象
        Page<Role> page =
                new Page<>(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        String queryString = queryPageBean.getQueryString();
        //创建条件构造器
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        //判断查询条件是否为空
        if (queryString!=null && queryString.length()>0){
            queryWrapper.eq("name",queryString)
                    .or().eq("keyword",queryString);
        }
        //执行查询
        Page<Role> rolePage = roleMapper.selectPage(page, queryWrapper);
        //创建返回结果对象
        PageResult pageResult = new PageResult(rolePage.getTotal(),rolePage.getRecords());
        return pageResult;
    }

    @Override
    public void save(Integer[] checkpermissionIds, Role role) {
        //添加角色，添加成功后主键会自动返回角色的id属性
        roleMapper.insert(role);
        //添加中间表
        for (Integer checkpermissionId : checkpermissionIds) {
            RolePermission rolePermission = new RolePermission(role.getId(),checkpermissionId);
            rolePermissionMapper.insert(rolePermission);
        }
    }

    @Override
    public Result getById(int id) {
        //查询角色的信息
        Role role = roleMapper.selectById(id);
        //查询这个角色有什么权限
        QueryWrapper<RolePermission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id",id);
        List<RolePermission> rolePermissions = rolePermissionMapper.selectList(queryWrapper);
        List<Integer> checkpermissionIds = new ArrayList<>();
        for (RolePermission rolePermission : rolePermissions) {
            checkpermissionIds.add(rolePermission.getPermissionId());
        }
        //把role和checkpermissionIds组装在一起
        RolePermissionVo rolePermissionVo = new RolePermissionVo(role,checkpermissionIds);
        return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS,rolePermissionVo);
    }

    //根据用户id查询角色信息
    @Override
    public List<Role> listByUserId(int id) {
        return roleMapper.listByUserId(id);
    }

    @Override
    public void update(Integer[] checkpermissionIds, Role role) {
        //更新角色，添加成功后主键会自动返回role的id属性
        roleMapper.updateById(role);
        //修改权限，把原来的删除(中间表记录)再重新添加
        QueryWrapper<RolePermission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id",role.getId());
        rolePermissionMapper.delete(queryWrapper);
        //重新添加中间表
        for (Integer checkpermissionId : checkpermissionIds) {
            RolePermission rolePermission = new RolePermission(role.getId(),checkpermissionId);
            rolePermissionMapper.insert(rolePermission);
        }
    }

}
